package com.java1234.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.java1234.dao.IBorrowDao;
import com.java1234.model.Book;
import com.java1234.model.BookType;
import com.java1234.model.Borrow;
import com.java1234.util.StringUtil;

public class BorrowImpl implements IBorrowDao {
	/**
	 * 添加借书信息到还书表
	 * 
	 * @param con
	 * @param bookType
	 * @return
	 * @throws Exception
	 */
	public int add(Connection con, Borrow borrow) throws Exception {
		String sql = "insert into t_returnbook values(?,?,?,?,?,?)";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, borrow.getBorrowerAccount());
		pstmt.setString(2, borrow.getBookId());
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

		Calendar c = Calendar.getInstance();

		c.setTime(new Date());

		Date d = c.getTime();

		String startTime = format.format(d);

		c.setTime(new Date());

		c.add(Calendar.MONTH, +3);

		Date m = c.getTime();

		String endTime = format.format(m);
		pstmt.setDate(3, java.sql.Date.valueOf(startTime));
		pstmt.setDate(4, java.sql.Date.valueOf(endTime));
		pstmt.setString(5, "已借出");
		pstmt.setInt(6, 0);
		int i = pstmt.executeUpdate();

		return i;

	}

	/**
	 * 查询图书借阅表集合
	 * 
	 * @param con
	 * @param bookType
	 * @return
	 * @throws Exception
	 */
	public ResultSet list(Connection con, Borrow borrow) throws Exception {
		StringBuffer sb = new StringBuffer("select * from t_borrow");
		if (StringUtil.isNotEmpty(borrow.getBookId())) {
			sb.append(" and bookId like '%" + borrow.getBookId() + "%'");
		}
		PreparedStatement pstmt = con.prepareStatement(sb.toString().replaceFirst("and", "where"));
		ResultSet i = pstmt.executeQuery();
		return i;

	}

	/**
	 * 更新图书状态
	 * 
	 * @param con
	 * @param user
	 * @return
	 * @throws Exception
	 */
	public int update(Connection con, Borrow borrow) throws Exception {

		String sql = "update t_borrow set bookStatus=? where bookId=?";
		PreparedStatement pstmt = con.prepareStatement(sql);

		pstmt.setString(1, "借出");
		pstmt.setString(2, borrow.getBookId());
		int i = pstmt.executeUpdate();

		return i;
	}

	/**
	 * 数据库指定借阅者信息下是否存在借阅记录
	 * 
	 * @param con
	 * @param bookTypId
	 * @return
	 * @throws Exception
	 */
	public boolean existBorrowByBorrower(Connection con, String borrowerAccount) throws Exception {
		String sql = "select * from t_returnbook where borrowerAccount=?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, borrowerAccount);
		ResultSet rs = pstmt.executeQuery();
		return rs.next();
	}

	/**
	 * 数据库指定图书下是否存在借阅记录
	 * 
	 * @param con
	 * @param bookTypId
	 * @return
	 * @throws Exception
	 */
	public boolean existBorrowByBookId(Connection con, String bookId) throws Exception {
		String sql = "select * from t_borrow where bookId=?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, bookId);
		ResultSet rs = pstmt.executeQuery();
		return rs.next();
	}
}
